package com.zsck.commonbase.widget;

import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.blankj.utilcode.util.ScreenUtils;
import com.zsck.commonbase.R;


public class BaseDialogFragment extends DialogFragment {

	public static final int MATCH_PARENT = -1;
	public static final int WRAP_CONTENT = -2;

	public static final int DEFAULT_GRAVITY = -100;

	private int mWidth;
	private int mHeight;
	private int mGravity;

	@SuppressLint("WrongConstant")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setStyle(R.style.CommonDialog, R.style.CommonDialog);
		onSetPosition();
	}

	protected int getScreenWidth() {
		return ScreenUtils.getScreenWidth();
	}

	protected int getScreenHeight() {
		return ScreenUtils.getScreenHeight();
	}

	protected void onCreateView() {
	}
	
	protected void onSetPosition() {
	}

	protected void setPosition(float width, float height, int gravity) {
		if (width == MATCH_PARENT)
			mWidth = getScreenWidth();
		else if (width > 0 && width < 1)
			mWidth = (int) (getScreenWidth() * width);
		else
			mWidth = (int) width;
		if (height == MATCH_PARENT)
			mHeight = getScreenHeight();
		else if (height > 0 && height < 1)
			mHeight = (int) (getScreenHeight() * height);
		else
			mHeight = (int) height;
		mGravity = gravity;
	}

	@Override
	public void onResume() {
		super.onResume();
		if (mGravity != DEFAULT_GRAVITY)
			getDialog().getWindow().setGravity(mGravity);
		if (mWidth == WRAP_CONTENT)
			mWidth = getDialog().getWindow().getAttributes().width;
		if (mHeight == WRAP_CONTENT)
			mHeight = getDialog().getWindow().getAttributes().height;
		getDialog().getWindow().setLayout(mWidth, mHeight);
	}
public  void showAllowingStateLoss(FragmentManager manager, String tag){
	FragmentTransaction ft = manager.beginTransaction();
	ft.add(this, tag);
	ft.commitAllowingStateLoss();
}
}
